package recommenRatings.domain;

import java.util.Calendar;
import java.util.Collection;
import java.util.List;

import recommenRatings.data.JDBCUserDAO;
import recommenRatings.utils.MD5Generator;

public class UserManager {

	private List<User> users;
	
	
	//TODO Hacer metodo que devuelva un user POID segun su nick;
	
	

	public UserManager() {

	}


	public void setUser(List<User> user) {
		this.users = user;
	}

	public Collection<User> getUsers() {
		return users;
	}

	public boolean login(String username, String password) {
		boolean res=false;
		String pass=MD5Generator.md5(password);
		String stmt = "WHERE ";
		if (username != null && username != "" && password !=null && password !="") {
			stmt += "username ='" + username + "' AND password='"+pass+"'";
		}
		users = (new JDBCUserDAO()).selectSearch(stmt);
		if(users.size()==1){
			res=true;
		}
		
		return res;

	}

	public void logout() {// TODO

	}

	public boolean registerUser(User u) {
		
		return new JDBCUserDAO().insert(u) ;

	}

	public boolean deleteUser() {
		return false;// TODO

	}

	public boolean modifyUser() {
		return false;// TODO

	}

	public Collection<User> select() {
		users = (new JDBCUserDAO()).select();
		return users;
	}

	public Collection<User> normalSearch(String userName, boolean gender,
			Integer lowAge, Integer highAge) {
		String stmt = "WHERE ";
		if (userName != null && userName != "") {
			stmt += "username LIKE'" + userName + "%' ";
		}
		// TODO crear genero en la BD y añadir la consulta aqui if(){
		// stmt+="username LIKE%"+userName+" ";
		// }
		if (lowAge != null && highAge != null) {
			Calendar c1 = Calendar.getInstance();
			Integer low = c1.get(Calendar.YEAR) - lowAge;
			Integer high = c1.get(Calendar.YEAR) - highAge;
			if (stmt.contains("username") || stmt.contains("gender")) {
				stmt += "AND ";
			}
			stmt += "birthdate BETWEEN '" + high + "-01-01 00:00:00' AND '"
					+ low + "-01-01 00:00:00'";
		}
		users = (new JDBCUserDAO()).selectSearch(stmt);
		return users;
	}

	public List<User> advancedSearch(String userName, Integer gender,
			Integer lowAge, Integer highAge, String state, String city,
			Collection<String> preferences) {
		// TODO Completar el metodo.
		String stmt = "WHERE ";
		if (userName != null && userName != "") {
			stmt += "username LIKE'" + userName + "%'  ";
		}
		if(gender!=null){
			if (stmt.contains("username")){
				stmt += "AND ";
			}
			stmt += "gender = "+ gender + " ";
		}
		
		
		if (lowAge != null && highAge != null) {
			Calendar c1 = Calendar.getInstance();
			Integer low = c1.get(Calendar.YEAR) - lowAge;
			Integer high = c1.get(Calendar.YEAR) - highAge;
			if (stmt.contains("username") || stmt.contains("gender")) {
				stmt += "AND ";
			}
			stmt += "birthdate BETWEEN '" + high + "-01-01 00:00:00' AND '"
					+ low + "-01-01 00:00:00' ";
		}
		if (state != null && state != "") {// TODO Crear State en la BD!!
			if (stmt.contains("username") || stmt.contains("gender")
					|| stmt.contains("birthdate")) {
				stmt += "AND ";
			}
			stmt += "state ='" + state + "' ";
		}
		if (city != null && city != "") {// TODO Crear City en la BD!!
			if (stmt.contains("username") || stmt.contains("gender")
					|| stmt.contains("birthdate") || stmt.contains("state")) {
				stmt += "AND ";
			}
			stmt += "city ='" + city + "' ";
		}
		// TODO Ver como hacer lo de las preferencias!!
		users = (new JDBCUserDAO()).selectSearch(stmt);
		return users;
	}
	
	public String userPOID(String s){
		JDBCUserDAO ju = new JDBCUserDAO();
		return ju.selectUserOID(s);
		
	}
	public User userbyPOID(String s){
		JDBCUserDAO ju = new JDBCUserDAO();
		return ju.selectUser(s);
		
	}

}
